package main.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcUtil {
	public  Connection connection;
	
	public JdbcUtil(){
		try{   
		    //加载MySql的驱动类   
		    Class.forName("com.mysql.jdbc.Driver") ;   
		}catch(ClassNotFoundException e){   
		    System.out.println("找不到驱动程序类 ，加载驱动失败！");   
		    e.printStackTrace() ;   
		}   
		 String url = "jdbc:mysql://localhost:3306/ngmtt?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance" ;    
//		 String url = "jdbc:mysql://localhost:3306/ngmtt_kfrh_gx" ;    
	     String username = "root" ;   
	     String password = "root" ;   
	     try{   
	    	 connection = DriverManager.getConnection(url , username , password ) ;   
	     }catch(SQLException se){   
	    	 System.out.println("数据库连接失败！");   
	    	 se.printStackTrace() ;   
	     }   
	}
	
	public void close(){
		try {
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*通过sql获取对应的数据*/
	public List<Map<String,Object>> queryForList(String sql){
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		Statement stmt;
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息，比如字段数、字段名等   
	        int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数   
	        Map<String,Object> rowData ; 
			while(rs.next()){
				 rowData = new HashMap<String, Object>();
	            for (int i = 1; i <= columnCount; i++) {   
	                    rowData.put(md.getColumnName(i), rs.getObject(i));   
	            }   
	            resultList.add(rowData);   
			}
			rs.close();
			//connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return resultList;
	}
	
	public List<String> queryForList(String sql,String colName){
		List<String> resultList = new ArrayList<String>();
		Statement stmt;
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息，比如字段数、字段名等   
	        int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数   
			while(rs.next()){
				String col="";
	            for (int i = 1; i <= columnCount; i++) {
	            		if(colName.equals(md.getColumnName(i))){
	            			resultList.add(rs.getObject(i).toString());   
	            		}
	            }   
			}
			rs.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				connection.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
        return resultList;
	}
	
	public int update(String sql){
		int resultInt = 0;
		Statement stmt;
		try {
			connection.setAutoCommit(false);;
			stmt = connection.createStatement();
			resultInt = stmt.executeUpdate(sql);
			connection.commit();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return resultInt;
	}
	
	public static void main(String[] args) {
		JdbcUtil jdbcUtil = new JdbcUtil();
		String sql="SELECT AUTHID from t_ucp_authelement WHERE SUPERCODE='000' AND AUTHNAME='常用操作'";
		System.out.println(sql);
		System.out.println(jdbcUtil.queryForList(sql,"AUTHID"));
	}
}
